/**
 * Created by Julia on 2016/05/04.
 */
define(['jquery', 'domReady!', 'appLocalStorage', 'appConst', 'model/global', 'model/project', 'dateUtils', 'common/widgetUtils', 'appMessageBox'], function ($, domReady, appLocalStorage, appConst, global, project, dateUtils, widgetUtils, msgBox) {

    var searchFields = {};
    var type = {};
    var components = {};
    var that;
    return {
        init: function () {
            that = this;
            components = {
                "wgt_report": $("#wid-id-1")
            };
        },
        getSearchFields: function () {
            return searchFields;
        },
        setSearchFields: function (obj) {
            searchFields = {};
            $.extend(searchFields, obj);
        },

        getType: function () {
            return type;
        },

        setType: function (obj) {
            $.extend(type, obj);
        },

        runDataTables: function () {
            var customParams = function (params) {
                $.extend(params, that.getSearchFields());
                params['draw'] = params.draw;
                params['sortField'] = "importTime";
                params['search_EQ_isDelete'] = false;
                return params;
            };
            var dt_testFiles = $('#dt_testFiles').dataTable({
                //"sDom": "<'dt-toolbar'<'col-xs-12 col-sm-6'><'col-sm-6 col-xs-12 hidden-xs'>r>" +
                //"t" +
                //"<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>",
                "sDom": "<'row'<'col-sm-12' t>> <'row'<'col-sm-5' i><'col-sm-7' p>>",
                "pageLength": 10,
                "paging": true,
                //"paging": true,
                //"pagingType":   "full_numbers",
                "autoWidth": false,
                "serverSide": true,
                "stateSave": true,
                "ordering": false,
                "language": appConst.table_language(),
                "ajax": {
                    "url": 'data/fileList',
                    "type": "POST",
                    "dataSrc": "body",
                    "traditional": true,
                    "data": customParams,
                    "error": function (e) {
                        if (e.status == 401) {
                            msgBox.sessionFailureBox();
                        }
                        if (e.status == 500) {
                            msgBox.serverFailureBox();
                        }
                        if (e.status == 404) {
                            msgBox.pathFailureBox();
                        }
                    }
                },
                //"processing": true,
                //"deferRender": true,
                "columns": [
                    {"data": null},
                    {"data": "id"},
                    {"data": "project.name"},
                    {"data": "originalName"},
                    {"data": "standardName"},
                    {"data": "savePath"},
                    {"data": "actor"},
                    {"data": "importTime"},
                    {"data": "status"},
                    {"data": null}
                ],
                "columnDefs": [
                    {
                        "targets": [1],
                        "visible": false
                    },
                    {
                        "targets": [2],
                        "render": function (data, type, full, meta) {
                            //$clamp(cell, {clamp: 1, useNativeClamp: false});
                            if(data == null) {
                                return "";
                            }
                            return type === 'display' && data.length > 40 ?
                            '<span title="'+data+'">' + data.substr(0, 38) + '...</span>' :
                                data;
                        }
                    },
                    {
                        "targets": [3],
                        "render": function (data, type, full, meta) {
                            //$clamp(cell, {clamp: 1, useNativeClamp: false});
                            if(data == null) {
                                return "";
                            }
                            return type === 'display' && data.length > 40 ?
                            '<span title="'+data+'">' + data.substr(0, 38) + '...</span>' :
                                data;
                        }
                    },
                    {
                        "targets": [4],
                        "render": function (data, type, full, meta) {
                            //$clamp(cell, {clamp: 1, useNativeClamp: false});
                            if(data == null) {
                                return "";
                            }
                            return type === 'display' && data.length > 40 ?
                            '<span title="'+data+'">' + data.substr(0, 38) + '...</span>' :
                                data;
                        }
                    },
                    {
                        "targets": [5],
                        "render": function (data, type, full, meta) {
                            //$clamp(cell, {clamp: 1, useNativeClamp: false});
                            if(data == null) {
                                return "";
                            }
                            return type === 'display' && data.length > 40 ?
                            '<span title="'+data+'">' + data.substr(0, 38) + '...</span>' :
                                data;
                        }
                    },
                    {
                        "targets": [6],
                        "render": function (data, type, full, meta) {
                            //$clamp(cell, {clamp: 1, useNativeClamp: false});
                            if(data == null) {
                                return "";
                            }
                            return type === 'display' && data.length > 40 ?
                            '<span title="'+data+'">' + data.substr(0, 38) + '...</span>' :
                                data;
                        }
                    },
                    {
                        "targets": [7],
                        "render": function (data, type, full, meta) {
                            //$clamp(cell, {clamp: 1, useNativeClamp: false});
                            return dateUtils.unix_to_datetime(data);
                        }
                    },
                    {
                        "targets": [8],
                        "render": function (data, type, full, meta) {
                            var status = '';
                            if (data == 'PROCESSING') {
                                status = '<span class="label label-warning">' + $.i18n.prop('DI_HISTORY_FILE_STATUS_PROCESSING') + '</span>';
                            } else if (data == 'DONE') {
                                status = '<span class="label label-success">' + $.i18n.prop('DI_HISTORY_FILE_STATUS_DONE') + '</span>';
                            }
                            return status;
                        }
                    },
                    {
                        "targets": [9],
                        "render": function (data, type, full) {
                            var id = full.testFileUUID;
                            var operate = "";
                            if (full.status == 'PROCESSING' || full.hasPermission != true) {
                                return operate;
                            }
                            var operate = '<div class="center">'
                                + '<a href="javascript:void(0);" data-id="' + id + '"  class="logicDeleteTestFileConfirm">'+ $.i18n.prop('GLOBAL_DELETE')+'</a>' + ' '
                                + '</div>';
                            return operate;
                        }
                    }
                ]
            });

            $("[rel=tooltip]").tooltip();
            /*Deal with the index number of column 1;*/
            global.drawNumber_bg($('#dt_testFiles'), function () {
                $("#dt_testFiles").find(".logicDeleteTestFileConfirm").unbind('click').on('click', function (obj) {
                    that.logicDeleteTestFileConfirm($(this).attr("data-id"));
                });
            });
        },
        logicDeleteTestFileConfirm: function (id) {
            msgBox.show({
                title: $.i18n.prop('MESSAGEBOX_CONFIRM_TITLE'),
                content: $.i18n.prop('MESSAGEBOX_CONFIRM_DELETE_CONTENT', $.i18n.prop('DI_TEST_FILE')) + " <p class='text-align-right'>" +
                "<button type='submit' data-id='"+ id +"' class='btn btn-primary btn-sm logicDeleteTestFile" + id + "'>"+$.i18n.prop('GLOBAL_BUTTON_YES')+"</button> " +
                "<a href='javascript:void(0);' class='btn btn-danger btn-sm'>"+$.i18n.prop('GLOBAL_BUTTON_NO')+"</a></p>",
                type: 'normal',
                timeout: false,
                clean: true
            });

            $('#divSmallBoxes').find(".logicDeleteTestFile" + id).unbind('click').on('click', function (e) {
                e.preventDefault();
                that.logicDeleteTestFile($(this).attr("data-id"));
            });
        },
        logicDeleteTestFile: function (id) {
            widgetUtils.toggleWidgetLoading(components.wgt_report);
            global.ajaxNoParams("data/logicDelete/" + id, function (data) {
                msgBox.successBox({
                    content: $.i18n.prop('GLOBAL_DELETE_SUCCESS', $.i18n.prop('DI_TEST_FILE'))
                });
                $('#dt_testFiles').DataTable().draw();
                widgetUtils.toggleWidgetLoading(components.wgt_report);

            }, function (data) {
                $('#dt_testFiles').DataTable().draw();
                widgetUtils.toggleWidgetLoading(components.wgt_report);

            });
        },
        /*Init Basic Search Event*/
        initBasicSearchEvent: function () {
            $('#btn-simple-search').click(function (e) {
                var obj = {};
                var fieldName = $("#search-input").attr("name");
                obj[fieldName] = $("#search-input").val();
                that.setSearchFields(obj);
                $('#dt_testFiles').DataTable().draw();
            });

           /* document.onkeydown=function(event){
                var e = event || window.event || arguments.callee.caller.arguments[0];
                if(e && e.keyCode==13){ // enter 键
                    var obj = {};
                    var fieldName = $("#search-input").attr("name");
                    obj[fieldName] = $("#search-input").val();
                    that.setSearchFields(obj);
                    $('#dt_testFiles').DataTable().draw();
                }
            };*/

        }
    };
});

